# -*- coding: utf-8 -*
# Create your views here.
from django.http import HttpResponse

from comment.models import Comment
from forms import CommentForm
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from django.shortcuts import render, get_object_or_404, redirect
from zhp_blog.models import *
def test(request):
    return HttpResponse('123')


def post_comment(request,hid):
    print hid
    post_info = request.POST
    body = post_info.get('body', '')
    try:
        if request.META.has_key('HTTP_X_FORWARD_FOR'):
            user_ip = request.META['HTTP_X_FORWARD_FOR']
        else:
            user_ip = request.META['REMOTE_ADDR']
    except:
        user_ip = ''
        print user_ip
    info = Comment(body=body, hid=hid,user_ip=user_ip)
    info.save()
    return redirect('/detail/%s/' % hid)


def del_comment(request,id,hid):
    '''删除评论'''
    print id
    id = int(id)
    if request.META.has_key('HTTP_X_FORWARD_FOR'):
        user_ip = request.META['HTTP_X_FORWARD_FOR']
    else:
        user_ip = request.META['REMOTE_ADDR']
    print user_ip
    try:
        help_content = Comment.objects.get(id=id,user_ip=user_ip)
    except Exception as e:
        print e
        return render(request, 'fail_comment.html')
    help_content.delete()
    # else:
    #     return render(request,'fail_comment.html')
    return redirect('/detail/%s/' % hid)



